package com.max.xihu.concurrent.day01;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @Author: Max
 * @Date: 2020-07-26 12:47
 * @Description:
 */
public class BankWaterService implements Runnable {

    private CyclicBarrier cb = new CyclicBarrier(4,this);

    private Executor executor = Executors.newFixedThreadPool(4);

    private ConcurrentHashMap<String,Integer> sheetBankCnt = new ConcurrentHashMap<>();

    private void count(){
        for (int i = 0; i < 4; i++){
            executor.execute(() -> {
                sheetBankCnt.put(Thread.currentThread().getName(),1);

                try {
                    cb.await();
                }catch (Exception e){
                    e.printStackTrace();
                }

            });
        }
    }

    @Override
    public void run() {
        int result = 0;

        for (Map.Entry<String,Integer> sheet : sheetBankCnt.entrySet()){
            result += sheet.getValue();
        }
        sheetBankCnt.put("result",result);
        System.out.println(result);

    }

    public static void main(String[] args) {
        BankWaterService service = new BankWaterService();
        service.count();
    }
}
